﻿#pragma execution_character_set("utf-8")
#include "Widget.h"
#include <Windows.h>

#include <QtWidgets/QMessageBox>

Widget::Widget(QWidget* parent)
	:QWidget(parent)
{
	setMinimumSize(QSize(720, 520));
	setWindowTitle("TCP/UDP Socket网络调试助手");
	
	mainLayout = new QGridLayout(this);
	left_layout = new QGridLayout();
	right_layout = new QGridLayout();
	top_layout = new QGridLayout();
	leftBox = new QGroupBox();
	topBox = new QGroupBox();
	listWidget = new QListWidget();
	w = new QStackedWidget();
	btn_creat = new QPushButton("创建");
	btn_delete = new QPushButton("删除");
	ldt_ip = new QLineEdit("127.0.0.1");
	ldt_ip->setMinimumWidth(160);
	ldt_port = new QLineEdit("60000");
	ldt_port->setMaximumWidth(80);
	QRegExp rx("^((2[0-4]\\d|25[0-5]|[01]?\\d\\d?)\\.){3}(2[0-4]\\d|25[0-5]|[01]?\\d\\d?)$");
	ldt_ip->setValidator(new QRegExpValidator(rx));
	connect(ldt_ip, &QLineEdit::editingFinished, this, &Widget::OnEditIpFinshed);

	connect(ldt_ip, &QLineEdit::textChanged, [=]() 
		{
			ip_true = false; 
		});
	//ldt_ip->setInputMask(QString("000.000.000.000;*"));
	comboBox = new QComboBox();
	comboBox->addItem("TCP服务器");
	comboBox->addItem("TCP客户端");
	comboBox->addItem("UDP客户端");
	comboBox->addItem("UDP群组");


	leftBox->setLayout(left_layout);
	topBox->setLayout(top_layout);
	
	
	mainLayout->addWidget(topBox, 0, 0, 1, 2);
	mainLayout->addWidget(leftBox, 1, 0);
	mainLayout->addLayout(right_layout, 1, 1);
	mainLayout->setColumnStretch(1, 1);

	top_layout->addWidget(btn_creat, 0, 5);
	top_layout->addWidget(btn_delete, 0, 6);
	top_layout->addWidget(new QLabel("IP地址:"), 0, 1);
	top_layout->addWidget(ldt_ip, 0, 2);
	top_layout->addWidget(new QLabel("端口号:"), 0, 3);
	top_layout->addWidget(ldt_port, 0, 4);
	top_layout->addWidget(comboBox, 0, 0);

	top_layout->setColumnStretch(7, 1);


	left_layout->addWidget(listWidget);
	right_layout->addWidget(w);

	listWidget->setMaximumWidth(180);
	listWidget->setMinimumWidth(180);

	connect(btn_creat, &QPushButton::clicked, this, &Widget::On_Creat);
	connect(btn_delete, &QPushButton::clicked, this, &Widget::On_Delete);
	connect(listWidget, &QListWidget::currentRowChanged, w,&QStackedWidget::setCurrentIndex);

	//----------------------------方便测试
	ldt_ip->setText("224.0.0.0");
	comboBox->setCurrentIndex(3);
	//-----------------------------------
}

void Widget::On_Creat()
{	
	QListWidgetItem* item = new QListWidgetItem("["+comboBox->currentText()+"] "+ldt_ip->text()+":"+ldt_port->text());
	QWidget* lb=nullptr;
	int port = ldt_port->text().toInt();
	QMessageBox msg(QMessageBox::Icon::Critical, "错误", "IP地址格式错误！", QMessageBox::Cancel|QMessageBox::Ok, NULL);
	switch (comboBox->currentIndex())
	{
	case 0://创建TCPserver
		if(port<65536&&port>0)
			lb = new TcpServer(port);
		break;
	case 1://创建TCPClient
		if (port < 65536 && port>0 && ip_true)
			lb = new TcpClient(ldt_ip->text(), port);
		else msg.exec();
		break;
	case 2:
		if (port < 65536 && port>0 && ip_true)
		{
			lb = new UDPSocket(ldt_ip->text(), port);
			ldt_port->setText("10000");
		}
		else msg.exec();
		break;
	case 3:
		if (port < 65536 && port>0 && ip_true)
		{
			lb = new UDPGroup(ldt_ip->text(), port);
			ldt_port->setText("10000");
		}
		else msg.exec();
		break;
	default:
		break;
	}
	if (lb)
	{
		item_list[item] = lb;
		listWidget->addItem(item);
		w->addWidget(lb);
	}
}

void Widget::On_Delete()
{
	QListWidgetItem* item = listWidget->currentItem();
	w->removeWidget(item_list[item]);
	item_list[item]->deleteLater();
	item_list.remove(item);
	listWidget->takeItem(listWidget->currentRow());
}

void Widget::OnEditIpFinshed()
{
	QStringList str_l = ldt_ip->text().split(".");
	if (str_l.count() == 4)
	{
		ip_true = true;
	}
}

void Widget::closeEvent(QCloseEvent* event)
{
	QMap<QListWidgetItem*, QWidget*>::iterator t = item_list.begin();
	while (t!=item_list.end())
	{
		t.value()->deleteLater();
		t++;
	}
}
